import { sms } from "@/api/data";
export default {
  data() {
    return {
      time: -1,
      txt: '获取',
      timer:null
    }
  },
  computed: {
    timeTxt() {
      return this.time < 0 ? this.txt : `${this.time}s`
    }
  },
  methods: {
    //获取code
    getCode(params){
      sms(params).then(res=>{
        this.time = 59;
        if(this.timer){
          this.clearTime()
          return false;
        }
        this.setTime()
      })
    },
    //清空倒计时状态
    clearTime(){
      clearTimeout(this.timer);
      this.time = -1;
      this.timer = null;
    },
    //开始倒计时
    setTime(){
      this.timer = setTimeout(() => {
        if(this.time==0) {
          this.clearTime()
          return false;
        }
        --this.time;
        this.setTime()
      }, 1000);
    }
  }
}